#include "graphics.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>

int main() {
	//导入股票价格数据
	char filename[] = "C:\\Users\\xiezhg5\\Desktop\\股票价格.csv";
	char table[16][5][40];
	char table2[16][8][40];
	FILE* fp = fopen(filename,"r");     // fp指向文件头部
	for(int i = 0 ; i < 16 ; i++)
		for(int j = 0 ; j < 5 ; j++) {
			fscanf(fp,"%[^,\n]",table[i][j]);  // 读到逗号或\n为止
			fgetc(fp); // 读取一个字符(逗号或\n)
		}
	/* 打印股票价格数据
	printf("某股票15天的价格数据为：\n");
	for(int i = 0 ; i < 16 ; i++) {
	for(int j = 0 ; j < 5 ; j++) {
		printf("%-20s",table[i][j]);   // -20:左对齐,占20个字符宽度
	}
	*/
	//建立股票特性数组
	strcpy(table2[0][5], "特性1");
	strcpy(table2[0][6], "特性2");
	strcpy(table2[0][7], "特性3");
	printf("该股票这15天的相应特性为：\n");
	for(int i = 0; i < 16; i++) {
		for(int j = 0; j < 5; j++) {
			strcpy(table2[i][j], table[i][j]);
		}
	}
	for(int i = 1; i < 16; i++) {
		if(atof(table[i][1]) < atof(table[i][4])) {
			strcpy(table2[i][5], "空心红蜡烛");
		} else if(atof(table[i][1]) > atof(table[i][4])) {
			strcpy(table2[i][5], "实心蓝白蜡烛");
		} else {
			strcpy(table2[i][5], "十字红蜡烛");
		}
	}
	for(int i = 1; i < 16; i++) {
		if(atof(table[i][3]) < atof(table[i][1]) && atof(table[i][3]) < atof(table[i][4])) {
			strcpy(table2[i][6], "有下影线");
		} else {
			strcpy(table2[i][6], "无下影线");
		}
	}
	for(int i = 1; i < 16; i++) {
		if(atof(table[i][2]) > atof(table[i][1]) && atof(table[i][2]) > atof(table[i][4])) {
			strcpy(table2[i][7], "有上影线");
		} else {
			strcpy(table2[i][7], "无上影线");
		}
	}
	//打印相应股票特性
	for(int i = 0 ; i < 16 ; i++) {
		for(int j = 0 ; j < 8; j++) {
			printf("%-20s",table2[i][j]);   // -20:左对齐,占20个字符宽度
		}
		printf("\n");
	}

	//计算平均值和方差
	double open_mean, high_mean, low_mean, close_mean;
	double open_var, high_var, low_var, close_var;
	double open_sum = 0, high_sum = 0, low_sum = 0, close_sum = 0;
	double open_sum2 = 0, high_sum2 = 0, low_sum2 = 0, close_sum2 = 0;
	for(int i = 1; i < 16; i++) {
		open_sum += atof(table[i][1]);
		high_sum += atof(table[i][2]);
		low_sum += atof(table[i][3]);
		close_sum += atof(table[i][4]);
	}

	open_mean = open_sum / 15;
	high_mean = high_sum / 15;
	low_mean = low_sum / 15;
	close_mean = close_sum / 15;

	for(int i = 1; i < 16; i++) {
		open_sum2 += (atof(table[i][1]) - open_mean) * (atof(table[i][1]) - open_mean);
		high_sum2 += (atof(table[i][2]) - high_mean) * (atof(table[i][2]) - high_mean);
		low_sum2 += (atof(table[i][3]) - low_mean) * (atof(table[i][3]) - low_mean);
		close_sum2 += (atof(table[i][4]) - close_mean) * (atof(table[i][4]) - close_mean);
	}

	open_var = open_sum2 / 15;
	high_var = high_sum2 / 15;
	low_var = low_sum2 / 15;
	close_var = close_sum2 / 15;

	printf("开盘价的平均值为：%lf，方差为:%lf\n", open_mean, open_var);
	printf("最高价的平均值为：%lf，方差为:%lf\n", high_mean, high_var);
	printf("最低价价的平均值为：%lf，方差为:%lf\n", low_mean, low_var);
	printf("收盘价的平均值为：%lf，方差为:%lf\n", close_mean, close_var);

	//判断股票价格趋势
	double cumlow_sum = 0, t;
	for(int i = 1; i < 15; i++) {
		t = atof(table[i+1][4]) - atof(table[i][4]);
		cumlow_sum += t;
	}
	printf("该股票收盘价的累计变化值为：%lf，因此\n", cumlow_sum);
	if(cumlow_sum > 0) {
		printf("该股票处于上涨趋势\n");
	} else if(cumlow_sum > 0) {
		printf("该股票处于下跌趋势\n");
	} else {
		printf("该股票处于横盘趋势\n");
	}

	//统计缺口天数
	int count = 0;
	for(int i = 1; i < 16; i++) {
		if(atof(table[i][2]) < atof(table[i+1][3])) {
			count++;
		}
	}
	printf("这15天内的缺口天数为：%d\n", count);

	//计算最大值和最小值
	double open_max = 0, high_max = 0, low_max = 0, close_max = 0;
	double open_min = 10000, high_min = 10000, low_min = 10000, close_min = 10000;
	for(int i = 1; i < 16; i++) {
		open_max = (open_max > atof(table[i][1]))? open_max : atof(table[i][1]);
		open_min = (open_min < atof(table[i][1]))? open_min : atof(table[i][1]);
		high_max = (high_max > atof(table[i][2]))? high_max : atof(table[i][2]);
		high_min = (high_min < atof(table[i][2]))? high_min : atof(table[i][2]);
		low_max = (low_max > atof(table[i][3]))? low_max : atof(table[i][3]);
		low_min = (low_min < atof(table[i][3]))? low_min : atof(table[i][3]);
		close_max = (close_max > atof(table[i][4]))? close_max : atof(table[i][4]);
		close_min = (close_min < atof(table[i][4]))? close_min : atof(table[i][4]);
	}
	printf("开盘价的最大值是：%lf，最小值是：%lf\n", open_max, open_min);
	printf("最高价的最大值是：%lf，最小值是：%lf\n", high_max, high_min);
	printf("最低价的最大值是：%lf，最小值是：%lf\n", low_max, low_min);
	printf("收盘价的最大值是：%lf，最小值是：%lf\n", close_max, close_min);

	//对收盘价进行冒泡排序
	char temp[15];
	for(int i = 1; i < 16; i++) {
		for(int j = 1; j < 16-i; j++) {
			if(strcmp(table[j][4],table[j+1][4]) > 0) {
				strcpy(temp,table[j][4]);
				strcpy(table[j][4],table[j+1][4]);
				strcpy(table[j+1][4],temp);
			}
		}
	}
	printf("收盘价的由小到大的排序结果为：\n");
	for(int i = 1; i < 16; i++) {
		puts(table[i][4]);
	}
	//绘制蜡烛图
	int cnt = 0;
	initgraph(800, 800); // 初始化，显示一个窗口
	for(int i = 1; i < 16; i++) {
		//填充矩形
		if(atof(table[i][1]) != atof(table[i][4])) {
			setfillcolor(EGEARGB(0xFF, 0xFF, 0x20, 0xA0));
			ege_fillrect(10 + 15 * cnt, 100*(atof(table[i][4])), 10, 100*(atof(table[i][4])-atof(table[i][1])));
		}
		if(atof(table[i][2]) > atof(table[i][1]) && atof(table[i][2]) > atof(table[i][4])) {
			setcolor(EGEACOLOR(0xFF, RED));
			ege_line(15 + 15 * cnt, 100*(atof(table[i][4])), 15 + 15 * cnt, 100*(atof(table[i][2])));
		}
		setcolor(EGEACOLOR(0xFF, RED));
		ege_line(15 + 15 * cnt, 100*(atof(table[i][1])), 15 + 15 * cnt, 100*(atof(table[i][3])));

		cnt++;
	}
	getch(); // 暂停一下等待用户按键
	closegraph(); // 关闭图形界面
	fclose(fp);
	return 0;
}